var getopt = require("posix-getopt");
var fs = require("fs");
var yaml = require("js-yaml");
var filename = "fp.json";

function usage() {
  console.log("usage:");
  console.log(process.argv[0] + " -i <FILE>");
  process.exit(1);
}

var parser, option, argvError;
parser = new getopt.BasicParser('i:(input)', process.argv);

while ((option = parser.getopt()) !== undefined) {
    switch (option.option) {
        case 'i' :
                filename = option.optarg;
                break;
        default :
		usage();
    }
}
 
fs.readFile(filename, 'utf8', function(err, data) {
  if (err) {
    console.log("An error occured");
  }
  yaml.loadAll(data, function(doc) {
    var out = "JSON; skip; x; y; hdg; spd";
    console.log(out);
    for (var i in doc) {
      var obj = doc[i];
      if (obj != null) {
	var sep = "; ";
 	var out = JSON.stringify(obj) + sep +
                  obj.skip + sep + obj.x + sep + obj.y + sep + 
 		  obj.hdg + sep + obj.spd;
        console.log(out);
      }
    }
  });
});

